package com.mall.goods.common.helper;

import com.fasterxml.jackson.core.type.TypeReference;
import com.mall.goods.common.dto.AdminMenuDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.util.List;

/**
 * Admin Session 辅助类
 *
 * @author yangfei
 */
public class AdminSessionHelper {
    /**
     * session前缀
     */
    private static String prefix = "ncSession_";

    public static int getAdminId() {
        Session session = SecurityUtils.getSubject().getSession();
        return (Integer) session.getAttribute(prefix + "adminId");
    }

    public static void setAdminId(int adminId) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(prefix + "adminId", adminId);
    }

    public static String getAdminName() {
        Session session = SecurityUtils.getSubject().getSession();
        return (String) session.getAttribute(prefix + "adminName");
    }

    public static void setAdminName(String adminName) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(prefix + "adminName", adminName);
    }

    public static String getAdminGroup() {
        Session session = SecurityUtils.getSubject().getSession();
        return (String) session.getAttribute(prefix + "adminGroup");
    }

    public static void setAdminGroup(String adminGroup) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(prefix + "adminGroup", adminGroup);
    }

    public static String getAdminAvatarUrl() {
        Session session = SecurityUtils.getSubject().getSession();
        return (String) session.getAttribute(prefix + "adminAvatarUrl");
    }

    public static void setAdminAvatarUrl(String adminAvatar) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(prefix + "adminAvatarUrl", adminAvatar);
    }

    public static List<AdminMenuDTO> getAdminMenu() {
        Session session = SecurityUtils.getSubject().getSession();
        return JsonHelper.toGenericObject((String)session.getAttribute(prefix + "adminMenu"), new TypeReference<List<AdminMenuDTO>>() {});
    }

    public static void setAdminMenu(List<AdminMenuDTO> adminMenu) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(prefix + "adminMenu", JsonHelper.toJson(adminMenu));
    }
}

